package com.appcenter.application.service;

import com.appcenter.application.mapper.DataMarksMapper;
import com.appcenter.application.mapper.DataVersionMapper;
import com.appcenter.application.model.DataVersion;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class DataVersionService {
    @Autowired
    DataVersionMapper dataVersionMapper;

    /**
     *  创建人:Coke
     *  备注: 获取当前生效的版本号信息
     *  时间: 2019/4/26 13:43
     *  参数:
     */
    //@Cacheable(value="getVersion",keyGenerator = "firstParamKeyGenerator")
    public Long getDataEffectiveVersion(String Genre) throws Exception {
        Example example = new Example(DataVersion.class);
        Example.Criteria criteriau = example.createCriteria();
        criteriau.andEqualTo("genre", Genre);
        BusinessLogger.Info(LoggerGenre.View, "获取当前生效的版本号信息", Genre);
        DataVersion curDataMarks = dataVersionMapper.selectOneByExample(example);
        return curDataMarks.getVersionNo();
    }
    //修改版本号
//    @CacheEvict(cacheNames="basedata", allEntries=true)
    /**
     *  创建人:Coke
     *  备注: 修改版本号
     *  时间: 2019/4/26 13:42
     *  参数:
     */
    public Boolean editDataEffectiveVersion(String Genre, Long newVersion) {
        try {
            Example example = new Example(DataVersion.class);
            Example.Criteria criteriau = example.createCriteria();
            criteriau.andEqualTo("genre", Genre);
            DataVersion curDataMarks = dataVersionMapper.selectOneByExample(example);
            curDataMarks.setVersionNo(newVersion);
            BusinessLogger.Info(LoggerGenre.Update, "修改版本号为" + newVersion, Genre);
            dataVersionMapper.updateByPrimaryKey(curDataMarks);
        } catch (Exception e) {
            throw new ErrorJsonResult("1000031", "失败");
        }
        return true;
    }


}
